/*
QAuthentee
Copyright (C) 2010  Pavel Mynarsky <admin@akia.se>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include "qfingerpushbutton.h"

QFingerPushButton::QFingerPushButton(QWidget *parent) : QAbstractButton(parent)
{
    setAttribute(Qt::WA_Hover, true);

    actionEnroll = new QAction("&Enroll", this);
    actionVerify = new QAction("&Verify", this);
    actionRemove = new QAction("&Remove", this);
    actionRemove->setDisabled(true);
    actionVerify->setDisabled(true);
    menu = new QMenu(this);
    enrolled = false;

    connect(actionEnroll, SIGNAL(triggered()), this, SIGNAL(enroll()));

    connect(actionVerify, SIGNAL(triggered()), this, SIGNAL(verify()));
    connect(actionRemove, SIGNAL(triggered()), this, SIGNAL(remove()));
}

void QFingerPushButton::setSkin(const QString& skin)
{
    m_skin = skin;
    QString base = ":/fingers/images/fingers/" + skin + '/';

    m_defaultSize = QSize();
    m_normal = QPicture();
    m_hovered = QPicture();
    m_pressed = QPicture();
    m_enrolled = QPicture();

    QSvgRenderer renderer;
    QPainter painter;

    if(renderer.load(base + "n.svg"))
    {
        m_defaultSize = renderer.defaultSize();
        painter.begin(&m_normal);
        renderer.render(&painter, QRectF(0.0, 0.0, 1.0, 1.0));
        painter.end();
    }

    if(renderer.load(base + "p.svg"))
    {
        painter.begin(&m_pressed);
        renderer.render(&painter, QRectF(0.0, 0.0, 1.0, 1.0));
        painter.end();
    }

    if(renderer.load(base + "h.svg"))
    {
        painter.begin(&m_hovered);
        renderer.render(&painter, QRectF(0.0, 0.0, 1.0, 1.0));
        painter.end();
    }

    if(renderer.load(base + "e.svg"))
    {
        painter.begin(&m_enrolled);
        renderer.render(&painter, QRectF(0.0, 0.0, 1.0, 1.0));
        painter.end();
    }

    updateGeometry();
    update();
}

QString QFingerPushButton::skin() const
{
    return m_skin;
}

QRect QFingerPushButton::buttonRect() const
{
    QSize buttonSize = m_defaultSize;
    buttonSize.scale(rect().size(), Qt::IgnoreAspectRatio);

    int buttonPosX = (rect().width() - buttonSize.width()) / 2;
    if (buttonPosX < 0) buttonPosX = 0;
    buttonPosX += rect().left();

    QPoint buttonPos = QPoint(buttonPosX, 0);

    return QRect(buttonPos, buttonSize);
}

void QFingerPushButton::paintEvent(QPaintEvent* event)
{
    Q_UNUSED(event);

    QPainter painter(this);
    painter.scale(buttonRect().width(), buttonRect().height());

    if(isDown())
        m_pressed.play(&painter);
    else if(underMouse())
        m_hovered.play(&painter);
    else if(enrolled)
        m_enrolled.play(&painter);
    else
        m_normal.play(&painter);
}

QSize QFingerPushButton::sizeHint() const
{
    if(!m_defaultSize.isEmpty())
        return m_defaultSize;
    else
        return QSize(100, 24);
}

void QFingerPushButton::contextMenuEvent(QContextMenuEvent *event)
{
    menu->addAction(actionEnroll);
    menu->addAction(actionVerify);
    menu->addAction(actionRemove);
    menu->popup(event->globalPos());
}

void QFingerPushButton::setEnrolled(bool isEnrolled)
{
    enrolled = isEnrolled;
    actionRemove->setEnabled(isEnrolled);
    actionVerify->setEnabled(isEnrolled);
}

